package com.hzhk.happyrent.network;

import android.util.Log;

import com.hzhk.happyrent.App;
import com.hzhk.happyrent.bean.rent.BaseBean;
import com.hzhk.happyrent.bean.rent.ProductListModel;
import com.hzhk.happyrent.bean.rent.ProductListNewsBean;
import com.hzhk.happyrent.bean.rent.RentHeaderPagerBean;
import com.hzhk.happyrent.bean.rent.TypesModel;
import com.hzhk.happyrent.key.Encryption;
import com.tamic.novate.Novate;
import com.tamic.novate.callback.ResponseCallback;
import com.tamic.novate.callback.RxStringCallback;
import com.tamic.novate.download.DownLoadCallBack;

import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

import static com.hzhk.happyrent.network.API.BASE_URL;

/**
 * User: hzhk(1097282555@qq.com)
 * Date: 2017/11/21
 * Time: 09:14
 * progject: HappyRent
 * Describe： ${Describe}
 */
public class HttpManager {
    private Map<String, Object> headers = new HashMap<>();
    private Novate novate;
    private static final boolean LOGOUT=false;
    public static final int TIMEOUT=8;

    private static class Singleton {
        static final HttpManager INSTANCE = new HttpManager();
    }
    private void getHeader(){
        headers.clear();
        headers.put("key", Encryption.riftKeyStone());
        headers.put("timestamps",Encryption.getTime());
        headers.put("version",API.VERSIONNAME);
        headers.put("platform","1");
//        Log.i("geturlHeader","key="+Encryption.riftKeyStone()
//                +"&timestamps="+Encryption.getTime()
//                +"&version="+API.VERSIONNAME
//                +"&platform=1");
    }
    private void novateSetting(){
        getHeader();
        novate = new Novate.Builder(App.getContext())
                .connectTimeout(TIMEOUT)
//                .addHeader(headers)
//                .addParameters(parameters)
                .baseUrl(BASE_URL)
                .addCache(false)
                .addLog(LOGOUT)
                .build();
    }


    private void logoutTag(String url,Map<String, Object> parameters) {
        if(parameters!=null&&!parameters.isEmpty()){
            String message = "";
            for (String key :
                    parameters.keySet()) {
                message=message+"&"+key+"="+parameters.get(key);
            }
            Log.i("geturl", BASE_URL+url+"?"+message.replaceFirst("&",""));
        }
    }

    public HttpManager(){
        novateSetting();
    }
    /**
     * perform_myApi_post
     * HostUrl
     */
    public void perform_post(String url, Map<String, Object> parameters, ResponseCallback myBaseSubscriber) {
        getHeader();
        parameters.putAll(headers);
        logoutTag(url,parameters);
        novate = new Novate.Builder(App.getContext())
                .connectTimeout(TIMEOUT)
//                .addParameters(parameters)
                .baseUrl(BASE_URL)
                .addLog(LOGOUT)
                .addCache(false)
                .build();
        novate.rxPost(url, parameters, myBaseSubscriber);
    }
    /**
     * perform_myApi_get
     * HostUrl
     */
    public void getRent(MyResponseCallback myBaseSubscriber) {
        getHeader();
        novate = new Novate.Builder(App.getContext())
                .connectTimeout(TIMEOUT)
                .baseUrl(BASE_URL)
                .addCache(false)
                .addParameters(headers)
                .addLog(LOGOUT)
                .build();
        RentApi myAPI = novate.create(RentApi.class);
        Observable<BaseBean> compose = Observable
                .concat(myAPI.getRentHeader("1")
                        ,myAPI.getProductType("1")
                        , myAPI.getGoodsList("1", "1", null, 1, 20)
                        , myAPI.getGoodNewsList("2", "1", null, 1, 20)
                )
                .map(new Func1<BaseBean, BaseBean>() {
                    @Override
                    public BaseBean call(BaseBean baseBean) {
                        if (baseBean instanceof RentHeaderPagerBean) {
                            baseBean.type = 0;
                        } else if (baseBean instanceof TypesModel) {
                            baseBean.type = 1;
                        } else if (baseBean instanceof ProductListModel) {
                            baseBean.type = 2;
                        } else if (baseBean instanceof ProductListNewsBean) {
                            baseBean.type = 3;
                        }
                        return baseBean;
                    }
                });
        novate.call(compose,myBaseSubscriber);
    }
    public void get(String url,Map<String,Object> parameters,RxStringCallback myBaseSubscriber) {
        getHeader();
        if(parameters==null){
            parameters=new HashMap<>();
        }
        parameters.putAll(headers);
        logoutTag(url,parameters);
        novate = new Novate.Builder(App.getContext())
                .connectTimeout(TIMEOUT)
                .baseUrl(BASE_URL)
                .addCache(false)
                .addLog(LOGOUT)
                .build();
        novate.rxGet(url,parameters,myBaseSubscriber);
    }
    public void getUpdate(String url,DownLoadCallBack subscriber) {
        getHeader();
        novate = new Novate.Builder(App.getContext())
                .connectTimeout(TIMEOUT)
                .baseUrl(BASE_URL)
                .addParameters(headers)
                .addCache(false)
                .addLog(LOGOUT)
                .build();
        novate.download(url,subscriber);
    }
    public static HttpManager getInstance() {
        return Singleton.INSTANCE;
    }
}
